// pages/shop-pendant/pendant.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    filterBox: false,
    select: 1,
    types: '',
    list: '',
    typeid: '',
    section: '',
    price_start: '',
    price_end: '',
    value:'',
    name:'',
  },
  // 获取类型
  gettypes: function () {
    ajax('/api/index/style_list', {
      data: {
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      for(var a in res.data){
        if (this.data.typeid == res.data[a].id){
          this.setData({
            name: res.data[a].name
          })
        }
      }
      this.setData({
        types: res.data
      })
    })
  },
  // 获取数据
  getshop: function () {
    ajax('/api/index/good_list', {
      data: {
        page: 1,
        limit: 20,
        style_id: this.data.typeid,
        price_start: this.data.price_start,
        price_end: this.data.price_end,
        new: this.data.select,
        name:this.data.value
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        list: res.data
      })
    })
  },
  // 搜索
  search:function(e){
    console.log(e.detail.value)
    this.setData({
      value: e.detail.value
    })
  },
  // 选择
  active: function (e) {
    var idx = e.currentTarget.dataset.index;
    this.setData({
      select: idx
    })
    this.getshop()
  },
  // 筛选类型id
  typeid: function (e) {
    var id = e.currentTarget.dataset.typeid;
    this.setData({
      typeid: id
    })
  },
  // 筛选区间
  section: function (e) {
    // console.log(e)
    var section = e.currentTarget.dataset.section;
    var sections = e.currentTarget.dataset.sections;
    var arr = sections.split('-')
    console.log(arr, 'arr')
    if (arr.length == 1) {
      if (arr[0] == '500以内') {
        this.setData({
          section: section,
          price_end: 500,
          price_start: ''
        })
      } else {
        this.setData({
          section: section,
          price_start: 50000,
          price_end: ''
        })
      }
    } else {
      this.setData({
        section: section,
        price_start: arr[0],
        price_end: arr[1]
      })
    }

  },
  // 显示筛选框
  filterBox: function () {
    this.setData({
      filterBox: true
    })
  },
  // 完成筛选框
  finish: function () {
    this.setData({
      filterBox: false
    })
    this.getshop()
  },
  // 重置
  reset: function () {
    this.setData({
      typeid: '',
      section: '',
      price_start: '',
      price_end: ''
    })
  },
  // 商品详情页面
  details: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/shop-details/details?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      typeid: options.style_id
    })
    
    this.getshop()
    this.gettypes()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})